<template>
  <UploadExcel :on-success="upload" />
</template>

<script>
import { importEmployee } from '@/api/employees.js'
export default {
  methods: {
    async upload(data) {
    //   console.log(data)
      //      user = 入职日期: 43535
      //     姓名: "高大山"
      //     工号: 20089
      //     手机号: 13041139879
    //   console.log(data)
      const newData = data.results.map((user) => {
        return this.Cn2En(user)
      })
      console.log(newData)
      await importEmployee(newData)
    },
    // Cn2En(user) {
    //   const userRelations = {
    //     '入职日期': 'timeOfEntry',
    //     '手机号': 'mobile',
    //     '姓名': 'username',
    //     '转正日期': 'correctionTime',
    //     '工号': 'workNumber'
    //   }
    //   const ojb = {}
    //   for (const key1 in user) {
    //     for (const key2 in userRelations) {
    //       if (key1 === key2) {
    //         ojb[key2] = user[key1]
    //       }
    //     }
    //   }
    //   return ojb
    // },
    Cn2En(user) {
      const ojb = {}
      const userRelations = {
        '入职日期': 'timeOfEntry',
        '手机号': 'mobile',
        '姓名': 'username',
        '转正日期': 'correctionTime',
        '工号': 'workNumber'
      }
      for (const key in user) {
        // 把中文对应的key改成userRelations对应的英文名
        // const mobile =  手机号
        let value = user[key]
        const enkey = userRelations[key]
        if (enkey === 'timeOfEntry' || enkey === 'correctionTime') {
          value = new Date(this.formatDate(value, '/'))
        }
        // 在对象中生成一个新属性，key为对应的英文名，key值为用user找到的原来的key
        ojb[enkey] = value
      }
      return ojb
    },
    formatDate(numb, format) {
      const time = new Date((numb - 1) * 24 * 3600000 + 1)
      time.setYear(time.getFullYear() - 70)
      const year = time.getFullYear() + ''
      const month = time.getMonth() + 1 + ''
      const date = time.getDate() - 1 + ''
      if (format && format.length === 1) {
        return year + format + month + format + date
      }
      return year + (month < 10 ? '0' + month : month) + (date < 10 ? '0' + date : date)
    }
  }
}
</script>

<style>

</style>
